package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.ocr.OcrResultInfo;
import com.ruoyi.common.utils.ocr.OcrUtils;
import com.ruoyi.system.domain.SysFileLog;
import com.ruoyi.system.service.ISysFileLogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysOcrTemplate;
import com.ruoyi.system.service.ISysOcrTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-11-29
 */
@RestController
@RequestMapping("/system/template")
public class SysOcrTemplateController extends BaseController
{
    @Autowired
    private ISysOcrTemplateService sysOcrTemplateService;

    @Autowired
    ISysFileLogService sysFileLogService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/ocrByFileIdAndTemplateId")
    public AjaxResult ocrByFileIdAndTemplateId(String fileId, String templateId)
    {
        SysFileLog fileLog = sysFileLogService.getById(fileId);
        File file = fileLog.getFile();
        List<OcrResultInfo> ocrResultInfos = OcrUtils.ocrJpg(file);
        SysOcrTemplate template = sysOcrTemplateService.selectSysOcrTemplateFullByTemplateId(new Long(templateId));
        Map<String, String> map = template.handleOcrData(ocrResultInfos);
        return success(map);
    }
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOcrTemplate sysOcrTemplate)
    {
        startPage();
        List<SysOcrTemplate> list = sysOcrTemplateService.selectSysOcrTemplateList(sysOcrTemplate);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:template:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOcrTemplate sysOcrTemplate)
    {
        List<SysOcrTemplate> list = sysOcrTemplateService.selectSysOcrTemplateList(sysOcrTemplate);
        ExcelUtil<SysOcrTemplate> util = new ExcelUtil<SysOcrTemplate>(SysOcrTemplate.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(sysOcrTemplateService.selectSysOcrTemplateByTemplateId(templateId));
    }
    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:template:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOcrTemplate sysOcrTemplate)
    {
        sysOcrTemplate.setCreateTime(new Date());
        int i = sysOcrTemplateService.insertSysOcrTemplate(sysOcrTemplate);
        sysFileLogService.uploadFileSuccess(sysOcrTemplate.getTemplateFileId());
        return AjaxResult.success();
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:template:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOcrTemplate sysOcrTemplate)
    {
        int i = sysOcrTemplateService.updateSysOcrTemplate(sysOcrTemplate);
        sysFileLogService.uploadFileSuccess(sysOcrTemplate.getTemplateFileId());
        return AjaxResult.success();
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:template:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(sysOcrTemplateService.deleteSysOcrTemplateByTemplateIds(templateIds));
    }
}
